/**
 * 
 */
package com.yst.metho.web.application.projectversion.actions.wbs;

import java.sql.SQLException;
import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.NoSuchObjectLocalException;
import javax.naming.NamingException;

import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ChangeEvent;
import nextapp.echo2.app.list.ListSelectionModel;

import com.yst.metho.web.application.SuiviProdApp;
import com.yst.metho.web.application.components.GenericEditWindow;
import com.yst.metho.web.application.components.listingview.ToolbarAction;
import com.yst.metho.web.application.projectversion.views.ActivityPane;
import com.yst.metho.web.application.projectversion.views.ProjectWBSListModel;
import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.activities.Activity;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.project.versions.ProjectVersion;
import com.yst.suivi.data.rights.Right;
import com.yst.suivi.status.StatusConstants;

/**
 * @author perso
 * 
 */
public class EditActivityAction extends ToolbarAction
{
	private ProjectWBSListModel mModel;

	private SelectField mProjectVersion;

	private Activity mSelectedActivity = null;

	private Project mPrj;

	/**
	 * 
	 */
	private static final String ACTION_COMMAND = "EditActivity";

	public EditActivityAction(ProjectWBSListModel model, SelectField projectVersion, Project prj)
	{
		super(Right.EDIT_ACTIVITY, "Edit");
		mModel = model;
		mPrj = prj;
		mProjectVersion = projectVersion;
		setActionCommand(ACTION_COMMAND);
		setEnabled(false);
		addActionListener(this);
	}

	/**
	 * Reaction aux differentes actions de l 'utilisateur(clic sur bouton,
	 * selection, ...)
	 */
	public void actionPerformed(ActionEvent e)
	{
		if (ACTION_COMMAND.equals(e.getActionCommand()))
		{
			try
			{
				mSelectedActivity.getNom();
				ProjectVersion version = (ProjectVersion) mProjectVersion.getSelectedItem();
				Collection folders = Locator.getFolderView().findFolder(mPrj.getId());
				ActivityPane view = new ActivityPane(version, mSelectedActivity, folders, Locator.getTeamView().getTeamRolesFor(version.getProject()), mSelectedActivity.getLot());
				GenericEditWindow window = new GenericEditWindow(view, "Modification de l'activite", new ModifyActivityBtn(mModel, view));
				window.setHeight(CreateActivityAction.ACTIVITY_HEIGHT);
				window.setWidth(CreateActivityAction.ACTIVITY_POPUP_WIDTH);
				SuiviProdApp.getApp().getDefaultWindow().getContent().add(window);
			}
			catch (FinderException e1)
			{
				ExceptionMgt.manage(e1);
			}
			catch (NamingException e1)
			{
				ExceptionMgt.manage(e1);
			}
			catch (CreateException e1)
			{
				ExceptionMgt.manage(e1);
			}
			catch (NoSuchObjectLocalException nsee)
			{
				ExceptionMgt.displayMessageBox("L'activitr selectionnee n'existe plus. La table va etre actualiser, si le probleme persiste contactez l'administrateur");
				mModel.refresh();
			}
			catch (SQLException e1)
			{
				ExceptionMgt.manage(e1);
			}
			catch (EJBException e1)
			{
				ExceptionMgt.manage(e1);
			}
		}
		else if (ProjectWBSListModel.PROJECT_LOT_SELECTION.equals(e.getActionCommand()))
		{
			if (-1 == ((SelectField) e.getSource()).getSelectedIndex())
			{
				setEnabled(false);
			}
			else
			{
				if (null != mSelectedActivity)
				{
					setEnabled(true);
				}
				else
				{
					setEnabled(false);
				}
			}
		}
		else if (ProjectWBSListModel.PROJECT_VERSION_SELECTION.equals(e.getActionCommand()))
		{
			setEnabled(false);
			mSelectedActivity = null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.ToolbarAction#stateChanged(nextapp.echo2.app.event.ChangeEvent)
	 */
	public void stateChanged(ChangeEvent arg0)
	{
		ListSelectionModel model = (ListSelectionModel) arg0.getSource();
		setEnabled(!model.isSelectionEmpty());
		if (!model.isSelectionEmpty())
		{
			int selectedIndex = model.getMinSelectedIndex();
			mSelectedActivity = (Activity) mModel.getValueAt(0, selectedIndex);
			if (StatusConstants.CLOSE == mSelectedActivity.getProjectVersion().getStatus())
			{
				setEnabled(false);
			}
		}
	}
}
